package component.auxiliares;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JComboBox;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JProgressBar;

import component.auxiliares.pojo.ProgressBarPojo;
import component.auxiliares.view.ProgressConfig;
import component.util.BaseMotionAdapter;

public class Progress extends JProgressBar {
	private JPopupMenu popupComponent = new JPopupMenu();
	private JMenuItem menuComponentRename = new JMenuItem("Renomear...");
	private JMenuItem menuComponentExclude = new JMenuItem("Excluir");
	private JMenuItem menuComponentProperties = new JMenuItem("Propriedades...");
	private JPanel panel;

	private ProgressBarPojo pojo;
	private MouseListener[] mouseListeners;
	private MouseMotionListener[] mouseMotionListeners;

	public Progress(JPanel panel, MouseEvent evt) {

		this.pojo = new ProgressBarPojo();

		this.setBounds(evt.getX(), evt.getY(), 60, 20);
		this.setSize(80, 20);
		this.panel = panel;
		panel.updateUI();
		panel.add(this);
		this.setStringPainted(true);
		this.setValue(50);
		new BaseMotionAdapter(this, panel);
		pojo.setX(evt.getX());
		pojo.setY(evt.getY());
		pojo.setText("Barra Gradual");

		pojo.setMax(255);

		this.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				txtFieldMouseClicked(evt);
			}
		});

		menuComponentRename.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				menuComponentRenameActionPerformed();
			}
		});
		menuComponentExclude.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				menuComponentExcludeActionPerformed();
			}
		});
		menuComponentProperties.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				menuComponentPropertiesActionPerformed();
			}
		});
		popupComponent.add(menuComponentRename);
		popupComponent.add(menuComponentExclude);
		popupComponent.add(menuComponentProperties);
	}

	private void menuComponentRenameActionPerformed() {
		String text = JOptionPane.showInputDialog("Renomear...");
		this.setString(text);
		pojo.setText(text);
		this.repaint();
	}

	public void disableListeners() {
		mouseListeners = this.getMouseListeners();
		mouseMotionListeners = this.getMouseMotionListeners();
		for (MouseListener ml : mouseListeners) {
			this.removeMouseListener(ml);
		}
		for (MouseMotionListener mml : mouseMotionListeners) {
			this.removeMouseMotionListener(mml);
		}
	}

	public void enableListeners() {
		for (MouseListener ml : mouseListeners) {
			this.addMouseListener(ml);
		}
		for (MouseMotionListener mml : mouseMotionListeners) {
			this.addMouseMotionListener(mml);
		}
	}

	private void menuComponentExcludeActionPerformed() {
		String[] options = { "Ok", "Cancelar" };
		int result = JOptionPane.showOptionDialog(null, "Você tem certeza?",
				"Excluir", JOptionPane.OK_CANCEL_OPTION,
				JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
		if (result == 0) {
			try {
				panel.remove(this);
				panel.repaint();
				this.finalize();
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
	}

	private void menuComponentPropertiesActionPerformed() {
		this.montaJanela();
	}

	public void setPojoConfig(String texto, Color color, JComboBox cmbTamanho,
			JComboBox cmbFonte) {
		// TODO
	}

	private void txtFieldMouseClicked(MouseEvent evt) {
		if (evt.getClickCount() == 2) {
			this.montaJanela();
		} else if (evt.getButton() == evt.BUTTON3) {
			popupComponent.show(this, evt.getX(), evt.getY());
		}
	}

	public void atualiza() {

		int value = (int) this.pojo.getVariable().getValue();

		int max = this.pojo.getMax();
		int min = this.pojo.getMin();

		int porcent = (value * 100) / (max - min);

		this.setValue(porcent);
		this.setString(String.valueOf(porcent) + "%");

	}

	private void montaJanela() {
		ProgressConfig frame = new ProgressConfig(this);
		frame.setVisible(true);
	}

	@Override
	public void setLocation(int x, int y) {
		super.setLocation(x, y);
		pojo.setX(x);
		pojo.setY(y);
	}

	public ProgressBarPojo getPojo() {
		return pojo;
	}

	public void setPojo(ProgressBarPojo pojo) {
		this.pojo = pojo;
	}

}
